const locationTrans = require('../../utils/util').locationTrans;
const server = require('../../utils/server');
const app = getApp();

let col1H = 0;
let col2H = 0;
Page({
	data: {
    filterId: 1,
    imgWidth: 0,
    scrollH: 0,
    col1: [],
    col2: [],
    pageIndex: 0,
    notMore: false,
		address: '定位中…',
		banners: [],
		icons: [
			[
				{
					id: 1,
					img: '/imgs/index/icon_12.jpg',
					name: '特色海鲜',
					url: ''
        },
        {
          id: 2,
          img: '/imgs/index/icon_16.jpg',
          name: '特色家常',
          url: ''
        },
				{
					id: 3,
					img: '/imgs/index/icon_15.jpg',
					name: '刺身类',
					url: ''
				},
				{
					id: 4,
					img: '/imgs/index/icon_3.jpg',
					name: '水果拼盘',
					url: ''
				},
				{
					id: 5,
					img: '/imgs/index/icon_4.jpg',
					name: '浓汤系列',
					url: ''
				},
				{
					id: 6,
					img: '/imgs/index/icon_7.jpg',
					name: '冷菜系列',
					url: ''
				},
				{
					id: 7,
					img: '/imgs/index/icon_5.jpg',
					name: '菜品榜单',
					url: ''
        },
        {
          id: 8,
          img: '/imgs/index/icon_9.jpg',
          name: '美味新菜',
          url: ''
        }
			]
		],
		shops: []
	},
  onShow: function(){
    //加载首组图片
    this.loadBroad();
    this.loadImages(false);
  },
	onLoad: function () {
		var self = this;
		wx.getLocation({
			type: 'gcj02',
			success: function (res) {
        locationTrans(res, (data)=>{
          if (data.status !==0) {
						self.setData({
              address: data.data.info.city
						});
					} else {
						self.setData({
							address: '定位失败'
						});
					}
        })
			}
		})

    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;

        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth
        });
      }
    })
	},
  loadBroad: function(){
    var that = this;
    server.postJSON('getBroad', { iswitch: true }, function (res) {
      that.setData({
        banners: res.data.data
      })
    });
  },
  loadImages: function(e){ 
    let that = this;
    var idx = this.data.pageIndex;
    if (!that.data.notMore){
      if (e) { // 滑动到底部时分页
        ++idx;
      } 
      server.postJSON('getMyCookList', { pageIndex: idx }, function (res) {
        var notMore = false;
        if(res.data.data.length<10){
          notMore=true;
        }
        that.setData({
          pageIndex: idx,
          notMore: notMore,
          shops: that.data.shops.concat(res.data.data)
        })
      });
    }
  },
  onImageLoad: function (e) {
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width;         // 图片原始宽度
    let oImgH = e.detail.height;        // 图片原始高度
    let imgWidth = this.data.imgWidth;  // 图片设置的宽度
    let scale = imgWidth / oImgW;       // 比例计算
    let imgHeight = oImgH * scale;      // 自适应高度

    let images = this.data.shops;
    let imageObj = {};

    for (let i = 0; i < images.length; i++) {
      let item = images[i];
      if (item._id == imageId) {
        imageObj.id = item._id;
        imageObj.src = item.imgs[0];
        break;
      }
    }
    imageObj.height = imgHeight;

    let loadingCount = this.data.loadingCount - 1;
    let col1 = this.data.col1;
    let col2 = this.data.col2;

    //判断当前图片添加到左列还是右列
    if (col1H <= col2H) {
      col1H += imgHeight;
      col1.push(imageObj);
    } else {
      col2H += imgHeight;
      col2.push(imageObj);
    }

    let data = {
      loadingCount: loadingCount,
      col1: col1,
      col2: col2
    };

    //当前这组图片已加载完毕，则清空图片临时加载区域的内容
    if (!loadingCount) {
      data.images = [];
    }
    this.setData(data);
  },
	onScroll: function (e) {
		if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
			this.setData({
				scrollDown: true
			});
		} else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
			this.setData({
				scrollDown: false
			});
		}
	},
	tapSearch: function () {
		wx.navigateTo({url: 'search'});
	},
	toNearby: function () {
		var self = this;
		self.setData({
			scrollIntoView: 'nearby'
		});
		setTimeout(function () {
			self.setData({
				scrollIntoView: ''
			});
		});
	},
	tapFilter: function (e) {
		switch (e.target.dataset.id) {
			case '1':
				this.data.shops.sort(function (a, b) {
					return a.id - b.id;
				});
				break;
			case '2':
				this.data.shops.sort(function (a, b) {
					return b.sales - a.sales;
				});
				break;
			case '3':
				this.data.shops.sort(function (a, b) {
					return a.distance - b.distance;
				});
				break;
		}
		this.setData({
			filterId: e.target.dataset.id,
			shops: this.data.shops
		});
	},
  openDetail: function (e) {
    wx.navigateTo({
      url: '/page/detail/index?_id=' + e.target.dataset.id,
    })
	}
});

